{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "using ExprOptimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1: R = x\n",
       "2: R = R * R\n",
       "3: R = R + R\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar = @grammar begin\n",
    "    R = x\n",
    "    R = R * R\n",
    "    R = R + R\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sample a random derivation tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"68.564pt\" height=\"96.472pt\" viewBox=\"0 0 68.564 96.472\" version=\"1.1\">\n",
       "<defs>\n",
       "<g>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-0\">\n",
       "<path style=\"stroke:none;\" d=\"\"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-1\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.84375 -3.046875 C 4.84375 -3.40625 4.5 -3.40625 4.375 -3.40625 L 2.953125 -3.40625 L 2.953125 -4.828125 C 2.953125 -4.9375 2.953125 -5.296875 2.609375 -5.296875 C 2.265625 -5.296875 2.265625 -4.9375 2.265625 -4.828125 L 2.265625 -3.40625 L 0.84375 -3.40625 C 0.734375 -3.40625 0.375 -3.40625 0.375 -3.046875 C 0.375 -2.703125 0.734375 -2.703125 0.84375 -2.703125 L 2.265625 -2.703125 L 2.265625 -1.28125 C 2.265625 -1.15625 2.265625 -0.8125 2.609375 -0.8125 C 2.953125 -0.8125 2.953125 -1.15625 2.953125 -1.28125 L 2.953125 -2.703125 L 4.375 -2.703125 C 4.5 -2.703125 4.84375 -2.703125 4.84375 -3.046875 Z M 4.84375 -3.046875 \"/>\n",
       "</symbol>\n",
       "<symbol overflow=\"visible\" id=\"glyph-1509053111390634-0-2\">\n",
       "<path style=\"stroke:none;\" d=\"M 4.9375 -0.296875 C 4.9375 -0.609375 4.6875 -0.609375 4.53125 -0.609375 L 4.140625 -0.609375 L 2.859375 -2.21875 L 4 -3.6875 L 4.390625 -3.6875 C 4.53125 -3.6875 4.8125 -3.6875 4.8125 -3.984375 C 4.8125 -4.296875 4.546875 -4.296875 4.390625 -4.296875 L 3.234375 -4.296875 C 3.078125 -4.296875 2.828125 -4.296875 2.828125 -4 C 2.828125 -3.6875 3.046875 -3.6875 3.3125 -3.6875 L 2.578125 -2.6875 L 1.828125 -3.6875 C 2.078125 -3.6875 2.296875 -3.6875 2.296875 -4 C 2.296875 -4.296875 2.046875 -4.296875 1.90625 -4.296875 L 0.734375 -4.296875 C 0.59375 -4.296875 0.328125 -4.296875 0.328125 -3.984375 C 0.328125 -3.6875 0.59375 -3.6875 0.734375 -3.6875 L 1.140625 -3.6875 L 2.3125 -2.21875 L 1.078125 -0.609375 L 0.671875 -0.609375 C 0.53125 -0.609375 0.265625 -0.609375 0.265625 -0.296875 C 0.265625 0 0.53125 0 0.671875 0 L 1.84375 0 C 2 0 2.25 0 2.25 -0.296875 C 2.25 -0.609375 2.03125 -0.609375 1.71875 -0.609375 L 2.578125 -1.828125 L 3.46875 -0.609375 C 3.1875 -0.609375 2.96875 -0.609375 2.96875 -0.296875 C 2.96875 0 3.21875 0 3.375 0 L 4.53125 0 C 4.671875 0 4.9375 0 4.9375 -0.296875 Z M 4.9375 -0.296875 \"/>\n",
       "</symbol>\n",
       "</g>\n",
       "</defs>\n",
       "<g id=\"surface1\">\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -3.083906 -6.163906 L -10.884687 -21.769375 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194695 1.595586 C -1.095521 0.99703 0.000661815 0.100928 0.298198 0.00174624 C 0.000660033 -0.0991703 -1.095539 -0.995253 -1.196463 -1.593807 \" transform=\"matrix(-0.44899,0.898,0.898,0.44899,37.56982,27.73534)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M 3.084063 -6.163906 L 11.134844 -22.269375 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194322 1.593946 C -1.096878 0.997132 -0.000679077 0.101049 0.296859 0.000132935 C -0.000677294 -0.0990489 -1.09686 -0.995151 -1.196034 -1.593707 \" transform=\"matrix(0.44899,0.898,0.898,-0.44899,59.58925,28.23348)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -17.255781 -34.511563 L -25.060469 -50.117031 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196342 1.592088 C -1.093689 0.995273 -0.000985773 0.0974303 0.300031 -0.0000115816 C 0.000752476 -0.0974481 -1.093706 -0.997011 -1.194631 -1.595565 \" transform=\"matrix(-0.44899,0.898,0.898,0.44899,23.39644,56.08214)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -11.091719 -34.511563 L -3.040937 -50.617031 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194625 1.591435 C -1.09544 0.9981 -0.000981394 0.0985377 0.298297 0.00110121 C 0.000760343 -0.0980806 -1.097162 -0.997663 -1.194596 -1.592739 \" transform=\"matrix(0.44899,0.898,0.898,-0.44899,45.41586,56.58028)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -31.427656 -62.859219 L -39.482344 -78.960781 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.194214 1.593837 C -1.095041 0.995281 0.00114231 0.0991792 0.298679 -0.00000266085 C 0.00114053 -0.100919 -1.095058 -0.997002 -1.195983 -1.595556 \" transform=\"matrix(-0.44899,0.898,0.898,0.44899,8.97395,84.9271)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.3985;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -25.263594 -62.859219 L -17.212812 -78.960781 \" transform=\"matrix(1,0,0,-1,48.455,5.965)\"/>\n",
       "<path style=\"fill:none;stroke-width:0.31879;stroke-linecap:round;stroke-linejoin:round;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;\" d=\"M -1.196676 1.59417 C -1.095752 0.995615 0.000447334 0.0995327 0.297986 -0.00138377 C 0.000449116 -0.100566 -1.095734 -0.996668 -1.194908 -1.595224 \" transform=\"matrix(0.44899,0.898,0.898,-0.44899,31.24245,84.9271)\"/>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-1\" x=\"45.84\" y=\"8.61\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-1\" x=\"31.667\" y=\"36.957\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-1\" x=\"17.494\" y=\"65.303\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-2\" x=\"3.321\" y=\"93.151\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-2\" x=\"31.667\" y=\"93.151\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-2\" x=\"45.84\" y=\"64.805\"/>\n",
       "</g>\n",
       "<g style=\"fill:rgb(0%,0%,0%);fill-opacity:1;\">\n",
       "  <use xlink:href=\"#glyph-1509053111390634-0-2\" x=\"60.013\" y=\"36.459\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n",
       "\n"
      ],
      "text/plain": [
       "TreeView.LabelledTree({7, 6} directed simple Int64 graph, Any[:+, :+, :+, :x, :x, :x, :x])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "tree = rand(RuleNode, grammar, :R, 4)\n",
    "display(tree, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the expression encoded by the tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":(((x + x) + x) + x)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex = get_executable(tree, grammar)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define ground truth expression for symbolic regression and the loss function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loss (generic function with 1 method)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ground_truth(x) = x^3 + x^2 + x\n",
    "function loss(tree::RuleNode, grammar::Grammar)\n",
    "    ex = get_executable(tree, grammar)\n",
    "    los = 0.0\n",
    "    global x\n",
    "    for x = -5.0:0.5:5.0\n",
    "        los += abs2(eval(ex) - ground_truth(x))\n",
    "    end\n",
    "    los\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Monte Carlo optimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search: \u001b[1mM\u001b[22m\u001b[1mo\u001b[22m\u001b[1mn\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m\u001b[1mC\u001b[22m\u001b[1ma\u001b[22m\u001b[1mr\u001b[22m\u001b[1ml\u001b[22m\u001b[1mo\u001b[22m \u001b[1mM\u001b[22m\u001b[1mo\u001b[22m\u001b[1mn\u001b[22m\u001b[1mt\u001b[22m\u001b[1me\u001b[22m\u001b[1mC\u001b[22m\u001b[1ma\u001b[22m\u001b[1mr\u001b[22m\u001b[1ml\u001b[22m\u001b[1mo\u001b[22ms\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "```\n",
       "MonteCarlo\n",
       "```\n",
       "\n",
       "Monte Carlo.\n",
       "\n",
       "# Arguments:\n",
       "\n",
       "  * `num_samples::Int`: number of samples\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n"
      ],
      "text/plain": [
       "```\n",
       "MonteCarlo\n",
       "```\n",
       "\n",
       "Monte Carlo.\n",
       "\n",
       "# Arguments:\n",
       "\n",
       "  * `num_samples::Int`: number of samples\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?MonteCarlo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(:((x * x + x) * x + x), 0.0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "p = MonteCarlo(10000, 5)\n",
    "results1 = optimize(p, grammar, :R, loss)\n",
    "(results1.expr, results1.loss)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Genetic Programming optimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search: \u001b[1mG\u001b[22m\u001b[1me\u001b[22m\u001b[1mn\u001b[22m\u001b[1me\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mc\u001b[22m\u001b[1mP\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mg\u001b[22m\u001b[1mr\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22m \u001b[1mG\u001b[22m\u001b[1me\u001b[22m\u001b[1mn\u001b[22m\u001b[1me\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mc\u001b[22m\u001b[1mP\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mg\u001b[22m\u001b[1mr\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22ms\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "```\n",
       "GeneticProgram\n",
       "```\n",
       "\n",
       "Genetic Programming.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `p_reproduction::Float64`: probability of reproduction operator\n",
       "  * `p_crossover::Float64`: probability of crossover operator\n",
       "  * `p_mutation::Float64`: probability of mutation operator\n",
       "  * `init_method::InitializationMethod`: initialization method\n",
       "  * `select_method::SelectionMethod`: selection method\n"
      ],
      "text/plain": [
       "```\n",
       "GeneticProgram\n",
       "```\n",
       "\n",
       "Genetic Programming.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `p_reproduction::Float64`: probability of reproduction operator\n",
       "  * `p_crossover::Float64`: probability of crossover operator\n",
       "  * `p_mutation::Float64`: probability of mutation operator\n",
       "  * `init_method::InitializationMethod`: initialization method\n",
       "  * `select_method::SelectionMethod`: selection method\n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?GeneticProgram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(:(x * (x * x) + (x + x * x)), 0.0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "p = GeneticProgram(1000,10,5,0.3,0.3,0.4)\n",
    "results2 = optimize(p, grammar, :R, loss)\n",
    "(results2.expr, results2.loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search: \u001b[1mG\u001b[22m\u001b[1mr\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mc\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22m\u001b[1mE\u001b[22m\u001b[1mv\u001b[22m\u001b[1mo\u001b[22m\u001b[1ml\u001b[22m\u001b[1mu\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mo\u001b[22m\u001b[1mn\u001b[22m \u001b[1mG\u001b[22m\u001b[1mr\u001b[22m\u001b[1ma\u001b[22m\u001b[1mm\u001b[22m\u001b[1mm\u001b[22m\u001b[1ma\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mc\u001b[22m\u001b[1ma\u001b[22m\u001b[1ml\u001b[22m\u001b[1mE\u001b[22m\u001b[1mv\u001b[22m\u001b[1mo\u001b[22m\u001b[1ml\u001b[22m\u001b[1mu\u001b[22m\u001b[1mt\u001b[22m\u001b[1mi\u001b[22m\u001b[1mo\u001b[22m\u001b[1mn\u001b[22ms\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "```\n",
       "GrammaticalEvolution\n",
       "```\n",
       "\n",
       "Grammatical Evolution.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `grammar::Grammar`: grammar\n",
       "  * `typ::Symbol`: start symbol\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iterations::Int`: number of iterations\n",
       "  * `init_gene_length::Int`: initial length of genotype integer array\n",
       "  * `max_gene_length::Int`: maximum length of genotype integer array\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `p_reproduction::Float64`: probability of reproduction operator\n",
       "  * `p_crossover::Float64`: probability of crossover operator\n",
       "  * `p_mutation::Float64`: probability of mutation operator\n",
       "  * `select_method::SelectionMethod`: selection method (default: tournament selection)\n",
       "  * `mutate_method::InitializationMethod`: mutation method (default: multi-mutate)\n"
      ],
      "text/plain": [
       "```\n",
       "GrammaticalEvolution\n",
       "```\n",
       "\n",
       "Grammatical Evolution.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `grammar::Grammar`: grammar\n",
       "  * `typ::Symbol`: start symbol\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iterations::Int`: number of iterations\n",
       "  * `init_gene_length::Int`: initial length of genotype integer array\n",
       "  * `max_gene_length::Int`: maximum length of genotype integer array\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `p_reproduction::Float64`: probability of reproduction operator\n",
       "  * `p_crossover::Float64`: probability of crossover operator\n",
       "  * `p_mutation::Float64`: probability of mutation operator\n",
       "  * `select_method::SelectionMethod`: selection method (default: tournament selection)\n",
       "  * `mutate_method::InitializationMethod`: mutation method (default: multi-mutate)\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?GrammaticalEvolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(:(x + (x + x * x) * x), 0.0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "p = GrammaticalEvolution(grammar,:R,1000,10,10,10,5,0.3,0.3,0.4; select_method=GrammaticalEvolutions.TruncationSelection(250))\n",
    "results3 = optimize(p, grammar, :R, loss)\n",
    "(results3.expr, results3.loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search: \u001b[1mC\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1ms\u001b[22m\u001b[1ms\u001b[22m\u001b[1mE\u001b[22m\u001b[1mn\u001b[22m\u001b[1mt\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mp\u001b[22m\u001b[1my\u001b[22m \u001b[1mC\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1ms\u001b[22m\u001b[1ms\u001b[22m\u001b[1mE\u001b[22m\u001b[1mn\u001b[22m\u001b[1mt\u001b[22m\u001b[1mr\u001b[22m\u001b[1mo\u001b[22m\u001b[1mp\u001b[22m\u001b[1my\u001b[22ms\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "```\n",
       "CrossEntropy\n",
       "```\n",
       "\n",
       "Cross Entropy method.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iiterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `top_k::Int`: top k elite samples used in selection\n",
       "  * `p_init::Float64`: initial value when fitting MLE\n",
       "  * `init_method::InitializationMethod`: Initialization method\n"
      ],
      "text/plain": [
       "```\n",
       "CrossEntropy\n",
       "```\n",
       "\n",
       "Cross Entropy method.\n",
       "\n",
       "# Arguments\n",
       "\n",
       "  * `pop_size::Int`: population size\n",
       "  * `iiterations::Int`: number of iterations\n",
       "  * `max_depth::Int`: maximum depth of derivation tree\n",
       "  * `top_k::Int`: top k elite samples used in selection\n",
       "  * `p_init::Float64`: initial value when fitting MLE\n",
       "  * `init_method::InitializationMethod`: Initialization method\n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?CrossEntropy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(:((x + x * (x * x)) + x * x), 0.0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(1)\n",
    "p = CrossEntropy(1000,10,5,500)\n",
    "results3 = optimize(p, grammar, :R, loss)\n",
    "(results3.expr, results3.loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "search:\n",
      "\n",
      "Couldn't find \u001b[36mPIPEParams\n",
      "\u001b[39mPerhaps you meant PIPEs\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "No documentation found.\n",
       "\n",
       "Binding `PIPEParams` does not exist.\n"
      ],
      "text/plain": [
       "No documentation found.\n",
       "\n",
       "Binding `PIPEParams` does not exist.\n"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "?PIPEParams"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(:((x + x * x) * x + x), 0.0)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "srand(0)\n",
    "p = PIPE(PPT(0.8),1000,10,0.2,0.1,0.05,1,0.2,0.6,0.999,10)\n",
    "results4 = optimize(p, grammar, :R, loss)\n",
    "(results4.expr, results4.loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
